feat(memtrack): support running the memory instrument in the macro-agent sandbox#443
Conversation
Greptile SummaryThis PR makes memory tracking work in the macro-agent sandbox. The main changes are:
Confidence Score: 4/5This is close, but the realloc tracking gap should be fixed before merging.
crates/memtrack/src/ebpf/c/memtrack.bpf.c Important Files Changed
|
Merging this PR will not alter performance
|
b3e73df to
b2f8ac7
Compare
b2f8ac7 to
94d981a
Compare
94d981a to
81c4b15
Compare
Make the memory instrument work when the runner and
codspeed-memtrackrun inside the macro-agent sandbox — unprivileged, in a user + PID namespace, with a delegated BPF token instead of root/file-capabilities. Companion to the platform PR that sets up the token and the exe-gate.Three things had to change:
Attach via
bpf()-native links. memtrack attached uprobes throughperf_event_openand a perf tracepoint, which a BPF token cannot authorize (they are gated onCAP_PERFMONin the init user namespace). Switch the allocation probes touprobe_multilinks andsched_process_forktotp_btf, both of which attach through thebpf()syscall a token covers. The eBPF logic is unchanged. Symbols are resolved to file offsets ourselves and attached by offset, since libbpf'suprobe_multisymbol resolution returnedENOENTfor libc allocator symbols (which silently captured zero events).Resolve PIDs relative to the tracker's PID namespace. Inside a PID namespace the PIDs memtrack registers are namespace-local while eBPF sees global PIDs, so nothing matched. Read PIDs relative to memtrack's own PID namespace (
bpf_get_ns_current_pid_tgidfor the current task, a CO-RE walk oftask->thread_pid->numbers[level]for the fork tracepoint). In the init namespace this resolves to global PIDs, preserving existing behavior.Accept a delegated BPF token as a privilege source. The memory executor required root or memtrack file-capabilities and otherwise bailed (and prompted for sudo). Treat a live
LIBBPF_BPF_TOKEN_PATHas satisfied privilege, and makegrant_privilegesa no-op when a token is present.The token half is transparent to memtrack — libbpf picks the token up from
LIBBPF_BPF_TOKEN_PATH; no explicit token code.Proven end-to-end on the macro-runner with the platform side:
codspeed run -m memoryon an instrumented divan bench runs inside the sandbox and uploads to staging (run6a4dae30d60d9ec8e172062b, peak 28.5 KB / 140 allocations).Refs COD-3047